//
//  SettingViewController.m
//  New
//
//  Created by dlios on 15-7-16.
//  Copyright (c) 2015年 yangTao. All rights reserved.
//

#import "SettingViewController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    BaseLabel *title =[[BaseLabel alloc]initWithFrame:CGRectMake(WIDTH/2 - 60, 5, 120, 40)];
    title.backgroundColor = [UIColor clearColor];
    self.navigationItem.titleView =title;
    title.text = @"设置";
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:20];
    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"123qw.png"] forBarMetrics:UIBarMetricsDefault];

  
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT - 22-44-49) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableV];
    self.tableV.bounces = NO;
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    // Do any additional setup after loading the view.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else{
    
        return 4;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *indent = @"cell";
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indent];
    if (cell == nil) {
        cell = [[SettingTableViewCell alloc]init];
    }

    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0 && indexPath.row == 0) {
       

        [cell bringString:@"收藏"];
    }else if (indexPath.section == 0 && indexPath.row == 1){

        [cell bringString:@"清除缓存"];
    
    }else if (indexPath.section == 0 && indexPath.row == 2){

        [cell bringString:@"夜间模式"];
       
        
        
        if (self.view.frame.size.width >= 375) {
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(cell.frame.size.width-12, 10, 50, 30)];
            view.layer.cornerRadius = 14;
            view.backgroundColor = [UIColor lightGrayColor];
            [cell addSubview:view];
            
            UISwitch *switchView =[[UISwitch alloc]initWithFrame:CGRectMake(cell.frame.size.width-12 , 10, 100, 40)];
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            if ([@"on"isEqualToString:[user objectForKey:@"night"]]) {
                
                switchView.on =YES;
            }
            else{
                switchView.on =NO;
                
            }
            
            
            [switchView addTarget:self action:@selector(SETbuttonAction:) forControlEvents:UIControlEventValueChanged];
            [cell addSubview:switchView];
  
        }else{
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(cell.frame.size.width-70, 10, 50, 30)];
            view.layer.cornerRadius = 14;
            view.backgroundColor = [UIColor lightGrayColor];
            [cell addSubview:view];
            
            UISwitch *switchView =[[UISwitch alloc]initWithFrame:CGRectMake(cell.frame.size.width-70 , 10, 100, 40)];
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            if ([@"on"isEqualToString:[user objectForKey:@"night"]]) {
                
                switchView.on =YES;
            }
            else{
                switchView.on =NO;
                
            }
            
            
            [switchView addTarget:self action:@selector(SETbuttonAction:) forControlEvents:UIControlEventValueChanged];
            [cell addSubview:switchView];

        
        }
        
    }else if (indexPath.section == 1 && indexPath.row == 0){
    

        [cell bringString:@"推荐好友使用"];
    }else if (indexPath.section == 1 && indexPath.row == 1){

        [cell bringString:@"意见反馈"];
    }else if (indexPath.section == 1 && indexPath.row == 2){
        

        [cell bringString:@"检查更新"];
    }else if (indexPath.section == 1 && indexPath.row == 3){
        
;
        
        [cell bringString:@"关于"];
    }
    
    cell.myDelegate = self;
    return cell;
    
}
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//return @" ";
//}



-(void)SETbuttonAction:(UISwitch *)Swith{

    
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSNotificationCenter *notification = [NSNotificationCenter defaultCenter];
    
//    if ([@"on"isEqualToString:[user objectForKey:@"night"]]) {
//        [notification postNotificationName:@"changeNight" object:@"off"];
//        
//    }
//    else{
//        [notification postNotificationName:@"changeNight" object:@"on"];
//    
//    }

    BOOL isButton = [Swith isOn];
    if (isButton) {
        [notification postNotificationName:@"changeNight" object:@"on"];

        
        }else{
        
     [notification postNotificationName:@"changeNight" object:@"off"];
    }
    

}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 45;
//
//}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT/10;
}

-(void)bringButton:(NSString *)string{

    if ([string isEqualToString:@"收藏"]) {
       
      StoreViewController   *storeVC = [[StoreViewController alloc]init];
        [self.navigationController pushViewController:storeVC animated:YES];
        
        
    }else if ([string isEqualToString:@"清除缓存"]){
       
        NSUInteger size = [[SDImageCache sharedImageCache]getSize];
        CGFloat mb = size/1024.0/1024.0;
        self.imageSize = [NSString stringWithFormat:@"缓存数据:%.2fMB",mb];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:self.imageSize message:@"是否清除缓存数据" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [self.tableV reloadData];
        
    }else if ([string isEqualToString:@"推荐好友使用"]){
        
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"55b09e1e67e58ede0f000a42"
                                          shareText:@"随时随地了解时事,只有我指尖资讯!!!"
                                         shareImage:[UIImage imageNamed:@"icon.png"]
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                           delegate:self];
        
        
        
    }else if ([string isEqualToString:@"意见反馈"]){
        UIAlertView *alView = [[UIAlertView alloc]initWithTitle:@"联系方式" message:@"邮箱:752677450@qq.com" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alView show];
        
    }
    else if ([string isEqualToString:@"检查更新"]){
        UIAlertView *alView = [[UIAlertView alloc]initWithTitle:nil message:@"已是最新版本(1.0.0)" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alView show];

       
    }else if ([string isEqualToString:@"关于"]){
        
        UIAlertView *alView = [[UIAlertView alloc]initWithTitle:@"欢迎来到指尖资讯" message:@"指尖资讯是专为用户倾情\n打造的最为便利的咨询客\n户端,用户可以随时随地的\n了解时事,与时俱进." delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alView show];
        
    }
    

}

// 清除缓存协议方法咕~~(╯﹏╰)b
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [[SDImageCache sharedImageCache]cleanDisk];
   
    if (buttonIndex ==1) {
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *cachpath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0];
            NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachpath];
            
            for (NSString *p in files) {
                NSError *error;
                NSString *path = [cachpath stringByAppendingPathComponent:p];
                if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                }
            }
            [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];    });
       }
}

-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
